package cn.tianyun.pethome.org.controller;

import cn.tianyun.pethome.basic.util.AjaxResult;
import cn.tianyun.pethome.basic.util.PageList;
import cn.tianyun.pethome.org.domain.Detail;
import cn.tianyun.pethome.org.query.DepartmentQuery;
import cn.tianyun.pethome.org.service.IDetilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DetailController {
    @Autowired
    private IDetilService detilService;

    @PostMapping
    public PageList<Detail> loadById(@RequestBody DepartmentQuery query){

        return  detilService.loadById(query);
    }
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Detail detail){
        System.out.println(detail);
        try {
            if(detail.getId()==null){
                System.out.println("=====>"+detail);
                detilService.add(detail) ;
                return AjaxResult.creat().setSuccess(true).setMsg("添加成功");
            }else{
                detilService.update(detail);
                return AjaxResult.creat().setSuccess(true).setMsg("修改成功");
            }
        }catch (Exception e ){
            return AjaxResult.creat().setSuccess(false).setMsg("失败");
        }
    }
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            detilService.delete(id);
            return AjaxResult.creat().setSuccess(true).setMsg("删除成功");
        }catch (Exception e){
            return AjaxResult.creat().setSuccess(false).setMsg("删除失败");
        }

    }
}
